package com.crm.system.controller.user;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.opportunityManager.Opportunity;
import com.crm.model.entity.sys.EducationalExperience;
import com.crm.model.entity.sys.UserInfo;
import com.crm.system.service.user.UserEducationalExperienceService;
import com.crm.system.service.user.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author baijian
 * @date 2021/3/17 14:26
 */
@RestController
@RequestMapping("/userInfo")
@Slf4j
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    UserEducationalExperienceService userEducationalExperienceService;

    /**
     * 个人信息-保存信息
     * @param userInfo 用户基本信息实体
     * @return
     */
    @PostMapping("/saveUserInfo")
    public DataResponse saveUserInfo(@RequestBody UserInfo userInfo) {
        Boolean result = userInfoService.saveUserInfo(userInfo);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 个人信息-保存图片
     * @param userInfo 用户基本信息实体
     * @return
     */
    @PostMapping("/saveUserInfoImg")
    public DataResponse saveUserInfoImg(@RequestBody UserInfo userInfo) {
        Boolean result = userInfoService.saveUserInfo(userInfo);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }







    /**
     * 修改回显用户基本信息
     * @param userId 用户id
     * @return
     */
    @PostMapping("/getUserInfoByUserId")
    public DataResponse getUserInfoByUserId(@RequestParam(value="userId",required = false) String userId){
        try{
            UserInfo userInfo = userInfoService.selectUserInfoByPrivateKey(userId);
            UserInfo userInfos = userInfoService.selectUserInfoByPrivateKey(userInfo.getUserSuperiorId());
            if(userInfos != null){
                userInfo.setUserSuperiorId(userInfos.getUserName());
            }else{
                userInfo.setUserSuperiorId("");
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),userInfo);
        }catch(Exception e){
            log.error("userInfo getUserInfoByUserId error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据用户id查询用户的下属
     * @param userId 用户id
     * @param onTheJobSituation 在职状态  0在职  1离职
     * @return
     */
    @PostMapping("/subordinateList")
    public DataResponse subordinateList(
            @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
            @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
            @RequestParam(value="sortName",required = false) String sortName,//排序列名字
            @RequestParam(value="sortOrder",required = false) String sortOrder,//排序(asc/desc)
            @RequestParam(value="userId",required = false) String userId,
            @RequestParam(value="onTheJobSituation",required = false) String onTheJobSituation){
        try{
            DhPageInfo<UserInfo> list = userInfoService.subordinateList(pageIndex,pageSize,sortName,sortOrder,userId,onTheJobSituation);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            log.error("userInfo subordinateList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据用户id查询教育经历
     * @param userId 用户id
     * @return
     */
    @PostMapping("/educationalExperienceList")
    public DataResponse educationalExperienceList(@RequestParam(value="userId",required = false) String userId){
        try{
            List<EducationalExperience> list = userEducationalExperienceService.educationalExperienceList(userId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            log.error("userInfo subordinateList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 个人信息-新增教育经历
     * @param educationalExperience 教育经历实体
     * @return
     */
    @PostMapping("/addAndEditEducationalExperience")
    public DataResponse addAndEditEducationalExperience(@RequestBody EducationalExperience educationalExperience) {
        Boolean result = userEducationalExperienceService.addAndEditEducationalExperience(educationalExperience);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 个人信息-修改教育经历回显
     * @param educationalExperienceId 常用语id
     * @return
     */
    @GetMapping("/getEducationalExperienceById")
    public DataResponse getEducationalExperienceById(@RequestParam(value="educationalExperienceId",required = false) String educationalExperienceId) {
        try{
            EducationalExperience educationalExperience = userEducationalExperienceService.getEducationalExperienceById(educationalExperienceId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),educationalExperience);
        }catch(Exception e){
            log.error("userInfo getEducationalExperienceById error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 个人信息-修改教育经历
     * @param educationalExperience 教育经历实体
     * @return
     */
    @PostMapping("/updateEducationalExperience")
    public DataResponse updateEducationalExperience(@RequestBody EducationalExperience educationalExperience) {
        Boolean result = userEducationalExperienceService.updateEducationalExperience(educationalExperience);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }




}
